But this is obviously very verbose. Also, we might not know how many times we want to print "Hello!" until we run our program.
This is why we use loops.
int i =0;while(i <10){system.Console.WriteLine("Hello!");
i++}
This would print "Hello!" 10 times.
You can see that a loop can be much easier to use to do something multiple times. In this case there is not that big of a difference, but imagine that you want to run several hundred lines of code several hundred times! This can be extremely time consuming without loops.
Incrementing Variables
Whoa! Wait... what was that "i++" thingy? Well it simply takes whatever number that is currently in i and adds 1 to it. The following statements are equal.
i = i + 1;
i++;
Neat little trick huh? You can also do i-- to decrement i. This will work with any variable, too, not just i (assuming the variable is an int or float).
While Loops
A 'while' loop executes a statement, or a block of statements wrapped in curly braces, repeatedly until the condition specified returns false. For instance, consider the following code
int a =0;while(a <3){System.Console.WriteLine(a);
a++;}
This would be your output:
0
1
2
Notice that this prints 0 first. Also, notice that 3 is never printed. On the last iteration through the loop, a++ increments a to 3 and returns to the beginning of the loop. At that point, however, a is NOT less than 3, so the block of code is NOT executed, and the while loop ends.
For Loops
For loops are similar but they have a little bit different way to write them.
For ( Variable Declaration/Initialization; condition; incrementing statement)
for(int a =0; a<3; a++){System.Console.WriteLine(a);}
This would be your output.
0
1
2
Note that you do not have to use a++ as your incrementing statement. You could do:
for(int a =0; a<3; a=a+5)
or any other similar statement. Also, feel free to use any other variables in your condition, and include &&'s and ||'s (and's and or's). For example:
for(int a =0;(a<3)&&(b<10); a=a+5)
Infinite Loop
There may be a time when you want something to happen again and again forever.
Such would be the case with the following while loop:
while(true){System.Console.WriteLine("Hello");}
This would write Hello an infinite number of times on the screen. While writing something on the screen infinitely is not something that is useful, you may want to check the value of a button or other condition infinitely so that you can say move a robot while a button is being pressed and stop the robot when that same button is not being pressed.
Beware of unintentional infinite loops as well... they will cause your program to never end! For example:
int v1 =0;int v2 =0;for(v1 =0; v1<10; v2++){System.Console.WriteLine(v1);}
Notice that we accidentally incremented v2 instead of v1 in our for-loop? This means that v1 will NEVER be anything other than 0.... and therefore our loop will continue forever.
Loops
<-- Prev --------- Contents --------- Next -->Loops allow you to run some lines of code several times over.
For example, I could run this code to write "Hello!" 10 times
But this is obviously very verbose. Also, we might not know how many times we want to print "Hello!" until we run our program.
This is why we use loops.
This would print "Hello!" 10 times.
You can see that a loop can be much easier to use to do something multiple times. In this case there is not that big of a difference, but imagine that you want to run several hundred lines of code several hundred times! This can be extremely time consuming without loops.
Incrementing Variables
Whoa! Wait... what was that "i++" thingy? Well it simply takes whatever number that is currently in i and adds 1 to it. The following statements are equal.
i = i + 1;
i++;
Neat little trick huh? You can also do i-- to decrement i. This will work with any variable, too, not just i (assuming the variable is an int or float).
While Loops
A 'while' loop executes a statement, or a block of statements wrapped in curly braces, repeatedly until the condition specified returns false. For instance, consider the following code
This would be your output:
0
1
2
Notice that this prints 0 first. Also, notice that 3 is never printed. On the last iteration through the loop, a++ increments a to 3 and returns to the beginning of the loop. At that point, however, a is NOT less than 3, so the block of code is NOT executed, and the while loop ends.
For Loops
For loops are similar but they have a little bit different way to write them.For ( Variable Declaration/Initialization; condition; incrementing statement)
This would be your output.
0
1
2
Note that you do not have to use a++ as your incrementing statement. You could do:
or any other similar statement. Also, feel free to use any other variables in your condition, and include &&'s and ||'s (and's and or's). For example:
Infinite Loop
There may be a time when you want something to happen again and again forever.
Such would be the case with the following while loop:
This would write Hello an infinite number of times on the screen. While writing something on the screen infinitely is not something that is useful, you may want to check the value of a button or other condition infinitely so that you can say move a robot while a button is being pressed and stop the robot when that same button is not being pressed.
Beware of unintentional infinite loops as well... they will cause your program to never end! For example:
Notice that we accidentally incremented v2 instead of v1 in our for-loop? This means that v1 will NEVER be anything other than 0.... and therefore our loop will continue forever.
<-- Prev --------- Contents --------- Next -->